<?php

declare(strict_types=1);

namespace longbingcore\permissions;

use app\diy\model\DiyModel;
use think\facade\Db;

/**
 * 小程序底部菜单
 * @author ArtizanZhang
 * @DataTime: 2019/12/6 18:56
 * Class Tabbar
 * @package longbingcore\permissions
 */
class Tabbar {

    /**
     * 根据权限来返回有权限的小程序底部菜单
     *
     * @param int $uniacid
     * @param int $user_id
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @author ArtizanZhang
     * @DataTime: 2019/12/10 15:56
     */
    static public function all (int $uniacid, int $user_id) : array {
        $diy =  DiyModel::where([['uniacid', '=', $uniacid], ['status', '=', 1]])->find();
        if (empty($diy)) {
            return  [];
        }
        $diy_tabbar =  json_decode($diy['tabbar'], true);


        //从查找没有权限的tabbarKey
        $denyTabbarKeys = [];
        $permissions = config('permissions');
        foreach ($permissions as $permissionClass) {
            if (!is_subclass_of($permissionClass, PermissionAbstract::class)) {
                continue;
            }

            /**
             * @var PermissionAbstract $permission
             */
            $permission = new $permissionClass($uniacid, $user_id);
            if (!$permission->cAuth($user_id) && !empty($permission->tabbarKey)) {
                $denyTabbarKeys[] = $permission->tabbarKey;
            }
            
        }

        //返回有权限的菜单
        foreach ($diy_tabbar['list'] as $k => $tabbar) {
            if (in_array($tabbar['key'], $denyTabbarKeys)) {
                unset($diy_tabbar['list'][$k]);
            }
        }

        return  $diy_tabbar;
    }


    /**
     * diy时，返回有diy权限的小程序菜单
     *
     * @param int $uniacid
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @author ArtizanZhang
     * @DataTime: 2019/12/10 15:56
     */
    static public function allForDiySelect (int $uniacid) : array {
        $diy_tabbar = [] ;


        $adminModelListInfo = config('app.AdminModelList') ;
        $saas_auth_admin_model_list =  $adminModelListInfo['saas_auth_admin_model_list'];


        foreach ($saas_auth_admin_model_list as $key=>$item) {

            $className =  'Permission' . ucfirst($key);
            $permissionPath = APP_PATH . $key . '/info/' . $className . '.php';
            if (file_exists($permissionPath) && require_once($permissionPath)) {

                $permissionClassName = 'app\\' . $key . '\\info\\'. $className;
                $permission = new $permissionClassName($uniacid , $item);

                if ( $permission->pAuth() &&  !empty($permission->adminMenuKey)) {

                    $diyTabbarPath = APP_PATH . $key . '/info/DiyTabbar.php';
                    if (file_exists($diyTabbarPath)) {

                        $tabbar = include_once ($diyTabbarPath) ;

                        $diy_tabbar = array_merge($diy_tabbar , $tabbar) ;
                    }
                }
            }
        }

        $data['list'] = $diy_tabbar;
        //默认设置
        $data['color'] = '#5d6268';
        $data['selectedColor'] = '#19c865';
        $data['backgroundColor'] = '#fff';
        $data['borderStyle'] = 'white';

        return  $data;
    }


    /**
     * @param $uniacid
     * @功能说明:获取有权限的默认数据
     * @author chenniang
     * @DataTime: 2020-05-15 17:28
     */
    static public function getAuthDefultTabbar($data){

        $url = config('app.AdminModelList')['diy_default_data']['tabbar'];
        //默认配置
        $url  = json_decode($url,true);

        foreach ($url['list'] as $k=>$v){

            if(!in_array($v['key'],$data)){

                unset($url['list'][$k]);
            }

        }

        $url['list'] = array_values($url['list']);

        return $url;
    }


    /**
     * @param $uniacid
     * @功能说明:获取有权限的
     * @author chenniang
     * @DataTime: 2020-05-15 17:50
     */
    static public function getAuthDefultPage($data){

        $url = config('app.AdminModelList')['diy_default_data']['page'];
        //默认配置
        $url  = json_decode($url,true);

        foreach ($url as $k=>$v){

            if(!in_array($k,$data)){

                unset($url[$k]);
            }

        }

        return $url;

    }


    /**
     * 获得拥有模块/app权限列表
     *
     * @param $uniacid
     * @return array
     * @author shuixian
     * @DataTime: 2019/12/20 13:39
     */
     public function getAuthList($uniacid){
        $denyAdminMenuKeys  = [] ;


        if(empty($uniacid)){
            return  $denyAdminMenuKeys ;
        }

        $adminModelListInfo = config('app.AdminModelList') ;
        $saas_auth_admin_model_list =  $adminModelListInfo['saas_auth_admin_model_list'];

        foreach ($saas_auth_admin_model_list as $key=>$item) {

            $className =  'Permission' . ucfirst($key);
            $permissionPath = APP_PATH . $key . '/info/' . $className . '.php';
            if (file_exists($permissionPath) && require_once($permissionPath)) {

                $permissionClassName = 'app\\' . $key . '\\info\\'. $className;
                $permission = new $permissionClassName($uniacid , $item);

                if ( $permission->pAuth() &&  !empty($permission->adminMenuKey)) {
                    $denyAdminMenuKeys[$key] = $item;
                }
            }

        }

        return $denyAdminMenuKeys ;
    }

}
